package com.sr.dao.book;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.sr.entity.book.BookList;
import com.sr.util.BaseDao;

public class BookDaoImp {
	
//查询所有图书
	public List<BookList> findAll(){
		List<BookList> list=new ArrayList<BookList>();
		Connection conn=BaseDao.getConnection();
		try {
			PreparedStatement ps=conn.prepareStatement("select * from booklist");
			ResultSet rs=ps.executeQuery();
			BookList bl=null;
			while(rs.next()){
				bl=new BookList();
				bl.setId(rs.getInt(1));
				bl.setBookName(rs.getString(2));
				bl.setAuthorName(rs.getString(3));
				bl.setListimg(rs.getString(4));
				bl.setCategory(rs.getString(5));
				list.add(bl);
				System.out.println(bl);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
		
	}
	//分页
	public List<BookList> findByPage(int pageNum,int pageSize){
		try {
			List<BookList> list = new ArrayList<BookList>();
			Connection conn = BaseDao.getConnection();
			PreparedStatement ps = conn.prepareStatement("select * from booklist limit ?,?");
			ps.setInt(1, (pageNum-1)*pageSize);
			ps.setInt(2, pageSize);
			ResultSet rs=ps.executeQuery();
			BookList bl=null;
			while(rs.next()){
				bl=new BookList();
				bl.setId(rs.getInt(1));
				bl.setBookName(rs.getString(2));
				bl.setAuthorName(rs.getString(3));
				bl.setListimg(rs.getString(4));
				bl.setCategory(rs.getString(5));
				list.add(bl);
				System.out.println(bl);
			}
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return null;
		
	}
	//页数
	public int findCountPage(){
		try {
			Connection conn = BaseDao.getConnection();
			PreparedStatement ps = conn.prepareStatement("select count(id) from booklist");
			ResultSet rs=ps.executeQuery();
			int count=0;
			while(rs.next()){
				count=rs.getInt(1);
			}
			return count;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 0;
		
	}
	//通过类别查询图书
	public BookList findByBookCategory(String category){
		Connection conn=BaseDao.getConnection();
		try {
			PreparedStatement ps=conn.prepareStatement("select * from booklist where category=?");
			ps.setString(1, category);
			ResultSet rs=ps.executeQuery();
			BookList bl=null;
			while(rs.next()){
				bl=new BookList();
				bl.setId(rs.getInt(1));
				bl.setBookName(rs.getString(2));
				bl.setAuthorName(rs.getString(3));
				bl.setListimg(rs.getString(4));
				bl.setCategory(rs.getString(5));
				System.out.println(bl);
			}
			return bl;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}
	//模糊查询
	public List<BookList> search(String bookname){
		List<BookList> list=new ArrayList<>();
		Connection conn=BaseDao.getConnection();
		try {
			PreparedStatement ps=conn.prepareStatement("select * from booklist where bookname like ?");
			ps.setString(1, "%"+bookname+"%");
			ResultSet rs=ps.executeQuery();
			BookList bl=null;
			while(rs.next()){
				bl=new BookList();
				bl.setId(rs.getInt(1));
				bl.setBookName(rs.getString(2));
				bl.setAuthorName(rs.getString(3));
				bl.setListimg(rs.getString(4));
				bl.setCategory(rs.getString(5));
				list.add(bl);
				System.out.println(bl);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
		
	}

}
